﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

namespace News.CYT369.Com.Domains.WebRequest
{
    public class HttpCookie : Dictionary<string, System.Net.Cookie> {
        public HttpCookie() { }

        public HttpCookie(System.Net.Cookie cookie) {
            this.SetCookie(cookie);
        }

        public HttpCookie(HttpCookie cookies) {
            this.SetCookie(cookies);
        }

        public HttpCookie(System.Net.CookieCollection cookies) {
            this.SetCookie(cookies);
        }

        public void SetCookie(System.Net.Cookie cookie) {
            if (cookie != null) {
                string fullname = string.Format("{0}/{1}/{2}", cookie.Domain, cookie.Name, cookie.Path);
                if (this.ContainsKey(fullname)) {
                    this[fullname] = cookie;
                } else {
                    this.Add(fullname, cookie);
                }
            }
        }

        public void SetCookie(HttpCookie cookies) {
            if (cookies != null) {
                foreach (var item in cookies) {
                    this.SetCookie(item.Value);
                }
            }
        }

        public void SetCookie(System.Net.CookieCollection cookies){
            if (cookies != null) {
                foreach (System.Net.Cookie cookie in cookies) {
                    this.SetCookie(cookie);
                }
            }
        }

        public System.Net.Cookie GetCookie(string domain, string name, string path = "/") {
            string fullname = string.Format("{0}/{1}/{2}", domain, name, path);
            if (this.ContainsKey(fullname)) {
                return this[fullname];
            }

            return null;
        }


    }
}
